﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using Microsoft.Phone.Tasks;
using SmartHYU_TestVersion.Notice;

namespace SmartHYU_TestVersion
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Define Menus
        List<Menus> newsMenus = new List<Menus>();
        List<Menus> lifeMenus = new List<Menus>();
        List<Menus> aboutMenus = new List<Menus>();
        List<HyuNoticeForm> hyuNotices = new List<HyuNoticeForm>();
        HyuNotice hyuNotice;
            

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }

            // Notice Menu
            hyuNotice = new HyuNotice();
            hyuNotice.GetRssDataFromWebAndStore();
            hyuNotice.DownloadNoticeCompleted += new HyuNotice.DownloadNoticeStatus(hyuNotice_DownloadNoticeCompleted);

            // News Menu
            // newsMenus.Add(new Menus("Weekly Hanyang", "Internet News", "Weekly.xaml"));
            newsMenus.Add(new Menus("HY news", "HYU Newspaper", "/News/HyuNews.xaml"));

            // Life Menu            
            lifeMenus.Add(new Menus("HYU Bob", "HYU restaurant menu provider", "/Life/Bob.xaml", "images/IconHyuBob.png"));
            lifeMenus.Add(new Menus("Yellow Page", "Yellow Page of HYU Depertments", "/Life/DepartmentInfo.xaml", "images/IconYellowPage.png"));
            lifeMenus.Add(new Menus("Maps", "HYU with Bingmap", "/Life/Map.xaml", "images/IconMaps.png"));

            // About Menu            
            aboutMenus.Add(new Menus("About Smart HYU 7", "ver 1.0 Public Beta", "/About/About.xaml"));
            aboutMenus.Add(new Menus("Developers", "Who made Smart HYU?", "/About/Devs.xaml"));
            aboutMenus.Add(new Menus("Hanyang Univ", "Do you know Hanyang University?", "/About/AboutHYU.xaml"));
            aboutMenus.Add(new Menus("Settings", "Application Settings", "/About/Settings.xaml"));

            
            // Binding
            Deployment.Current.Dispatcher.BeginInvoke(() => 
                {
                    noticeListBox.ItemsSource = hyuNotice.LoadNoticeFromLocalStorage();
                    newsMenuListBox.ItemsSource = newsMenus;
                    lifeMenuListBox.ItemsSource = lifeMenus;
                    AboutMenu.ItemsSource = aboutMenus;
                }
            );
        }

        void hyuNotice_DownloadNoticeCompleted(bool completeStatus, string noticeXml)
        {
            hyuNotices = hyuNotice.LoadNoticeFromLocalStorage();
            noticeListBox.ItemsSource = hyuNotices;
        }


        /**************************************************** 
         *
         * Touch Events (Menu Select)
         * 
         ***************************************************/

        private void NewsMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            NavigationService.Navigate(
                new Uri(
                    newsMenus.Skip(newsMenuListBox.SelectedIndex).First().PageUrl, 
                    UriKind.Relative
                    )
                );
            newsMenuListBox.SelectedIndex = -1;
        }

        private void lifeMenuListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            NavigationService.Navigate(
                new Uri(
                    lifeMenus.Skip( lifeMenuListBox.SelectedIndex).First().PageUrl, 
                    UriKind.Relative)
                );
            lifeMenuListBox.SelectedIndex = -1;
        }

        private void AboutMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            NavigationService.Navigate(
                new Uri(
                    aboutMenus.Skip(AboutMenu.SelectedIndex).First().PageUrl, 
                    UriKind.Relative)
                );
            AboutMenu.SelectedIndex = -1;
        }

        private void noticeListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            WebBrowserTask noticeViewer = new WebBrowserTask();
            noticeViewer.URL = hyuNotices[noticeListBox.SelectedIndex].Url;
            noticeViewer.Show();
            // noticeListBox.SelectedIndex = -1;// commented out for escaping error
        }


        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBox.Show("Do you really want to out of campus?", "After School", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                e.Cancel = true;
        }

        
    }

        /*************************************************
        *
        * Classes for Menu
        * 
        ***************************************************/


    public class Menus 
    {
        public string MainTitle { get; set; }
        public string SubTitle { get; set; }
        public string PageUrl { get; set; }
        public string IconUrl { get; set; }


        /********************************************
         *           Constructors
         ********************************************/
        public Menus() 
        {
            // nothing to do
        }

        public Menus(string mainTitle, string subTitle, string pageUrl) 
        {
            this.MainTitle = mainTitle;
            this.SubTitle = subTitle;
            this.PageUrl = pageUrl;
            //this.IconUrl = "NoIcon.png";
            this.IconUrl = "images/IconDefault.png";
        }

        public Menus(string mainTitle, string subTitle, string pageUrl, string iconUrl) 
        {
            this.MainTitle = mainTitle;
            this.SubTitle = subTitle;
            this.PageUrl = pageUrl;
            this.IconUrl = iconUrl;
        }
        /********************************************
         *           END : Constructors
         ********************************************/


    }
}